
import { limitEnum } from '@/services/jobs';
/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser | undefined }) {
    const { currentUser } = initialState || {};

    const menuTree: {
        can_overview: boolean,
        can_basicConfig: boolean,
        can_delivery: boolean,
        can_order: boolean,
        can_num: boolean,
        can_company: boolean,
        can_postHouse: boolean,
        can_imei: boolean,
        can_cabinet: boolean,
        can_agent: boolean,
        can_deliveryAll: {
            search: boolean,
            changePhone: boolean,
            backUp: boolean,
            return: boolean,
            sendSms: boolean,
            sendCab: boolean,
        },
        can_postHouseAll: {
            search: boolean,
            update: boolean,
        },
        can_imeiAll: {
            search: boolean,
            create: boolean,
            delete: boolean,
        },
        can_numAll: {
            search: boolean,
            create: boolean,
            update: boolean,
            delete: boolean,
        },
        can_companyAll: {
            search: boolean,
            create: boolean,
            update: boolean,
        },
        can_cabinetAll: {
            search: boolean,
            create: boolean,
            update: boolean,
            delete: boolean,
        },
        can_agentAll: {
            search: boolean,
            create: boolean,
            update: boolean,
            delete: boolean,
        }
    } = (currentUser?.isAdmin === 1 || currentUser?.isRoot === 1) ? {
        can_overview: true,
        can_basicConfig: true,
        can_delivery: true,
        can_order: true,
        can_num: true,
        can_company: true,
        can_postHouse: true,
        can_imei: true,
        can_cabinet: true,
        can_agent: true,
        can_deliveryAll: {
            search: true,
            changePhone: true,
            backUp: true,
            return: true,
            sendSms: true,
            sendCab: true,
        },
        can_postHouseAll: {
            search: true,
            update: true,
        },
        can_imeiAll: {
            search: true,
            create: true,
            delete: true,
        },
        can_numAll: {
            search: true,
            create: true,
            update: true,
            delete: true,
        },
        can_companyAll: {
            search: true,
            create: true,
            update: true,
        },
        can_cabinetAll: {
            search: true,
            create: true,
            update: true,
            delete: true,
        },
        can_agentAll: {
            search: true,
            create: true,
            update: true,
            delete: true,
        }
    } : {
            can_overview: false,
            can_basicConfig: false,
            can_delivery: false,
            can_order: false,
            can_num: false,
            can_company: false,
            can_postHouse: false,
            can_imei: false,
            can_cabinet: false,
            can_agent: false,
            can_deliveryAll: {
                search: false,
                changePhone: false,
                backUp: false,
                return: false,
                sendSms: false,
                sendCab: false,
            },
            can_postHouseAll: {
                search: false,
                update: false,
            },
            can_imeiAll: {
                search: false,
                create: false,
                delete: false,
            },
            can_numAll: {
                search: false,
                create: false,
                update: false,
                delete: false,
            },
            can_companyAll: {
                search: false,
                create: false,
                update: false,
            },
            can_cabinetAll: {
                search: false,
                create: false,
                update: false,
                delete: false,
            },
            can_agentAll: {
                search: false,
                create: false,
                update: false,
                delete: false,
            }
        };
    currentUser && currentUser.isAdmin !== 1 && currentUser.isRoot !== 1 && currentUser.menuURIList?.forEach((item: string) => {
        const menuItem = limitEnum.find(i => i.value === +item);
        if (menuItem) {
            switch (menuItem.key) {
                case 'deliveryAll':
                    if (+item === 110101) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 110102) { menuTree[`can_${menuItem.key}`].changePhone = true; }
                    if (+item === 110103) { menuTree[`can_${menuItem.key}`].backUp = true; }
                    if (+item === 110104) { menuTree[`can_${menuItem.key}`].return = true; }
                    if (+item === 110105) { menuTree[`can_${menuItem.key}`].sendSms = true; }
                    if (+item === 110106) { menuTree[`can_${menuItem.key}`].sendCab = true; }
                    break;
                case 'postHouseAll':
                    if (+item === 120101) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120102) { menuTree[`can_${menuItem.key}`].update = true; }
                    break;
                case 'imeiAll':
                    if (+item === 120401) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120402) { menuTree[`can_${menuItem.key}`].create = true; }
                    if (+item === 120403) { menuTree[`can_${menuItem.key}`].delete = true; }
                    break;
                case 'numAll':
                    if (+item === 120201) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120202) { menuTree[`can_${menuItem.key}`].create = true; }
                    if (+item === 120203) { menuTree[`can_${menuItem.key}`].update = true; }
                    if (+item === 120204) { menuTree[`can_${menuItem.key}`].delete = true; }
                    break;
                case 'companyAll':
                    if (+item === 120301) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120302) { menuTree[`can_${menuItem.key}`].create = true; }
                    if (+item === 120303) { menuTree[`can_${menuItem.key}`].update = true; }
                    break;
                case 'cabinetAll':
                    if (+item === 120501) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120502) { menuTree[`can_${menuItem.key}`].create = true; }
                    if (+item === 120503) { menuTree[`can_${menuItem.key}`].update = true; }
                    if (+item === 120504) { menuTree[`can_${menuItem.key}`].delete = true; }
                    break;
                case 'agentAll':
                    if (+item === 120601) { menuTree[`can_${menuItem.key}`].search = true; }
                    if (+item === 120602) { menuTree[`can_${menuItem.key}`].create = true; }
                    if (+item === 120603) { menuTree[`can_${menuItem.key}`].update = true; }
                    if (+item === 120604) { menuTree[`can_${menuItem.key}`].delete = true; }
                    break;
                default:
                    menuTree[`can_${menuItem.key}`] = true;
                    return;
            }
        }
    });

    return {
        // 驿站运营后台权限
        canStation: true,
        // 管理员
        canAdmin: currentUser && currentUser.isAdmin === 1,
        // 超管(暂未用到)
        canRoot: currentUser && currentUser.isRoot === 1,
        canShow: false,
        isStaff: !!currentUser && currentUser.role === 3, // TODO: 员工
        isMaster: !!currentUser && currentUser.role === 2,    // TODO: 店长
        ...menuTree,
    };
}
